package hongcaosp.app.android.search;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.modle.bean.UserInfo;
import xlj.lib.android.base.utils.GlideUtils;
import xlj.lib.android.base.view.OnRecycleItemClickListener;

public class SearchUserHolder extends BaseHolder<UserInfo> {

    private TextView desc,userName,fansCount,followBtn;
    private ImageView userLogo;
    private Context context;
    private UserFollowCall userFollowCall;
    public SearchUserHolder(Context context, ViewGroup parent, OnRecycleItemClickListener recycleItemClickListener, UserFollowCall userFollowCall) {
        super(LayoutInflater.from(context).inflate(R.layout.item_search_user,parent,false), recycleItemClickListener);

        this.context = context;
        this.userFollowCall = userFollowCall;
        desc = itemView.findViewById(R.id.descrip);
        userLogo = itemView.findViewById(R.id.user_logo);
        userName = itemView.findViewById(R.id.user_name);
        fansCount = itemView.findViewById(R.id.product_fans);
        followBtn = itemView.findViewById(R.id.follow_btn);

        followBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getData() != null) {
                    if (getData().isMeFollowmTa()) {
                        if (SearchUserHolder.this.userFollowCall != null) {
                            SearchUserHolder.this.userFollowCall.onUserUnfollow(SearchUserHolder.this);
                        }
                    } else {
                        if (SearchUserHolder.this.userFollowCall != null) {
                            SearchUserHolder.this.userFollowCall.onUserFollow(SearchUserHolder.this);
                        }
                    }
                }
            }
        });
    }

    @Override
    public void setData(UserInfo userInfo) {
        super.setData(userInfo);
        GlideUtils.loadCircleImage(context,userInfo.getUserLogo(),userLogo,R.drawable.icon_head);
        desc.setText(userInfo.getUserComment());
        userName.setText(userInfo.getNickName());
        fansCount.setText("粉丝：" + userInfo.getFansCount());

        freshFollow();
    }

    public void freshFollow() {
        if(getData().meFollowTa()) {
            followBtn.setSelected(true);
            followBtn.setText("已关注");
        } else {
            followBtn.setSelected(false);
            followBtn.setText("关注");
        }
    }

    public interface UserFollowCall {
        void onUserFollow(SearchUserHolder searchUserHolder);
        void onUserUnfollow(SearchUserHolder searchUserHolder);
    }
}
